package com.ymttest.database.mongo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import com.ymt.core.tool.FormatUtil;
import com.ymt.database.MongoDBDriver;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;

public class MQMessageCompensateWapper {
	MongoDBDriver mdd;

	public MQMessageCompensateWapper() throws Exception {
		
		}

	/**
	 * 用uuid查询
	 * 
	 * @param uid
	 * @return AccessToken
	 * @throws Exception
	 */
	public MQMessageCompensateBean getCompensateByUuid(String appName,String codeName,String uuid) throws Exception {
		
		mdd = new MongoDBDriver(EnvSetup.getSystemUrl("set5_mongoip_noauth"), Integer.valueOf(EnvSetup
				.getSystemUrl("set5_mongoport_noauth")), "JMQ_Message_Compensate");
		mdd.selectCollection("Mq_"+appName+"_"+codeName);
	
		String re = null;
		MQMessageCompensateBean mb = null;

		ArrayList<String> list = mdd.selectPartLimit("{\"uuid\":\"" + uuid + "\"}", 1);
		try {
			if (list.size() > 0) {
				re = list.get(0);
				mb = (MQMessageCompensateBean) FormatUtil.GSONToObject(re, MQMessageCompensateBean.class);
				return mb;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 用uuid查询
	 * 
	 * @param uid
	 * @return AccessToken
	 * @throws Exception
	 */
	public MQMessageCompensateBean getCompensateByMid(String appName,String codeName,String mid) throws Exception {
		
		mdd = new MongoDBDriver(EnvSetup.getSystemUrl("set5_mongoip_noauth"), Integer.valueOf(EnvSetup
				.getSystemUrl("set5_mongoport_noauth")), "JMQ_Message_Compensate");
		mdd.selectCollection("Mq_"+appName+"_"+codeName);
	
		String re = null;
		MQMessageCompensateBean mb = null;

		ArrayList<String> list = mdd.selectPartLimit("{\"mid\":\"" + mid + "\"}", 1);
		try {
			if (list.size() > 0) {
				re = list.get(0);
				mb = (MQMessageCompensateBean) FormatUtil.GSONToObject(re, MQMessageCompensateBean.class);
				return mb;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public boolean update(String appName,String codeName,Map<String, Object> searchMap,Map<String, Object> updateMap) throws Exception{
		
		mdd = new MongoDBDriver(EnvSetup.getSystemUrl("set5_mongoip_noauth"), Integer.valueOf(EnvSetup
				.getSystemUrl("set5_mongoport_noauth")), "JMQ_Message_Compensate");
		mdd.selectCollection("Mq_"+appName+"_"+codeName);
		
		return  mdd.update(searchMap, updateMap).isUpdateOfExisting();
	}

	public static void main(String args[])
	{
		try {
			
			Date dNow = new Date(); // 当前时间
	        Calendar calendar1 = Calendar.getInstance(); // 得到日历
	        calendar1.setTime(dNow);// 把当前时间赋给日历
	        calendar1.add(Calendar.MINUTE, -10); // 设置为前一天
	        Date dBefore = calendar1.getTime(); // 得到前一天的时间
	        
			new MQMessageCompensateWapper().update("testbamei", "zaiqusi", MapUtil.hashMap("mid", "6828914239"), MapUtil.hashMap("rtime", dBefore));
			
//			MQMessageCompensateBean bean = new MQMessageCompensateWapper().getCompensateByUuid("testbamei", "youqusi", "201608.57b557468e6ced1f1463bbc6");
//			System.out.println(bean.getAppid());
//			
//			MQMessageCompensateBean bean2 = new MQMessageCompensateWapper().getCompensateByMid("testbamei", "youqusi", "5817684661");
//			System.out.println(bean2.getAppid());
//			System.out.println(bean2.getBody());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
